﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DAL;
public partial class UnityProperty : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        databind();
    }
    void databind()
    {
        using (DataClassesDataContext lqdb = new DataClassesDataContext())
        {
            tvShow.DataSource = lqdb.css_dwxz;
            tvShow.KeyFieldName = "xzbh";
            tvShow.DataBind();
        }
    }
    //删除
    protected void tvShow_NodeDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        using (DataClassesDataContext lqdb = new DataClassesDataContext())
        {
            var result = lqdb.css_dwxz.FirstOrDefault(r => r.xzbh == e.Keys[0].ToString());
            lqdb.css_dwxz.DeleteOnSubmit(result);
            lqdb.SubmitChanges();
        }
        e.Cancel = true;
        tvShow.CancelEdit();
        databind();
    }
    //添加
    protected void tvShow_NodeInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        using (DataClassesDataContext lqdb = new DataClassesDataContext())
        {
            string XZBH = "";
            string xzbhp = tvShow.FocusedNode["xzbh"].ToString();
            string ParentID = tvShow.FocusedNode.ParentNode.Level.ToString();
            css_dwxz dwxz = new css_dwxz();
            if (e.NewValues["xzbh"] != null)
                XZBH = e.NewValues["xzbh"].ToString();
            string msg = CheckPartCode(XZBH);
            if (msg == "false")
            {
                throw new Exception("编号必须为两位数字！");
            }
            var result = lqdb.css_dwxz.FirstOrDefault(r => r.xzbh == xzbhp + XZBH);
            if (result != null)
            {
                throw new Exception("对不起该编号已经存在！");
            }
            else
            {
                if (e.NewValues["sjbh"] != null)
                    dwxz.sjbh = e.NewValues["sjbh"].ToString();
                dwxz.xzbh = xzbhp + XZBH;
                if (e.NewValues["xzmc"] != null)
                    dwxz.xzmc = e.NewValues["xzmc"].ToString();
                lqdb.css_dwxz.InsertOnSubmit(dwxz);
                lqdb.SubmitChanges();
            }

        }
        e.Cancel = true;
        tvShow.CancelEdit();
        databind();
    }
    //更新
    protected void tvShow_NodeUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        using (DataClassesDataContext lqdb = new DataClassesDataContext())
        {
            var result = lqdb.css_dwxz.FirstOrDefault(r => r.xzbh == e.Keys[0].ToString());
            if (result != null)
                result.xzmc = e.NewValues["xzmc"].ToString();
            lqdb.SubmitChanges();
        }
        e.Cancel = true;
        tvShow.CancelEdit();
        databind();
    }

    protected string CheckPartCode(string PartNum)
    {
        string message = "";
        System.Text.RegularExpressions.Regex num = new System.Text.RegularExpressions.Regex(@"^[0-9]{2}$");
        if (!num.IsMatch(PartNum))
        {
            message = "false";
        }
        else
        {
            message = "true";
        }
        return message;
    }
}